//
//  DiscoverController.m
//  SimpleHappy
//
//  Created by lanou on 15/11/23.
//  Copyright © 2015年 RockyFung. All rights reserved.
//

#import "DiscoverController.h"
#import "Define.h"
#import "StrategyController.h"
#import "PlayerController.h"
#import "FieldController.h"
#import "PlaceViewController.h"


@interface DiscoverController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UISegmentedControl *segment;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic,retain) UIButton *placeButton;//显示地方的button
@property (nonatomic,retain) UIButton *imageButton;//显示图标的button
@property(nonatomic,copy)NSString *place;

@property (nonatomic, strong) UIView *segView;


@end

// segment高度
#define segmentHeight 30*KScreenWidth/375

@implementation DiscoverController

- (void)viewWillAppear:(BOOL)animated
{
    
    NSString *cityName = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];
//    [self.placeButton setTitle:cityName forState:UIControlStateNormal];
    
    if (cityName)
    {
        [self.placeButton setTitle:cityName forState:UIControlStateNormal];
        
    }
    else
    {
        [self placeButtonAction];
        return;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationItem.title=@"SimpleHappy";
    self.navigationController.navigationBar.translucent = NO;
//    self.navigationController.hidesBarsOnSwipe = YES;
    
    
    // 标题格式
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth / 7.5, KScreenWidth / 4.68)];
    titleLabel.text = @"SimpleHappy";
    titleLabel.font = [UIFont fontWithName:@"SnellRoundhand-Bold" size:KScreenWidth / 18.75];
    self.navigationItem.titleView = titleLabel;
    
    
    // 添加scrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    // 大scrollView尺寸
    self.scrollView.contentSize = CGSizeMake(KScreenWidth * 3, 0);
    // 设置偏移量
    self.scrollView.contentOffset = CGPointMake(0, 0);
    self.scrollView.pagingEnabled = YES; // 整平滑动
    self.scrollView.bounces = NO;
    // 关闭滚动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    // 设置代理
    self.scrollView.delegate = self;
    [self.view addSubview:_scrollView];
  
    
    
    // 攻略
    StrategyController *strategy = [[StrategyController alloc]initWithStyle:UITableViewStylePlain];
    strategy.tableView.frame = CGRectMake(0, segmentHeight , KScreenWidth, KScreenHeight );
    [self.scrollView addSubview:strategy.tableView];
    [self addChildViewController:strategy];
    
//    // 玩家
    PlayerController *player = [[PlayerController alloc]initWithStyle:UITableViewStylePlain];
    player.tableView.frame = CGRectMake(KScreenWidth, segmentHeight, KScreenWidth, KScreenHeight);
    [self.scrollView addSubview:player.tableView];
    [self addChildViewController:player];
//
//    // 视野
    FieldController *field = [[FieldController alloc]initWithStyle:UITableViewStyleGrouped];
    field.tableView.frame = CGRectMake(KScreenWidth * 2 , segmentHeight, KScreenWidth, KScreenHeight);
    [self.scrollView addSubview:field.tableView];
    [self addChildViewController:field];
    
    
    NSArray *array = @[@"攻略",@"玩乐",@"视野"];
    self.segment = [[UISegmentedControl alloc]initWithItems:array];
    self.segment.frame = CGRectMake((KScreenWidth- KScreenWidth/1.2) / 2, 0, KScreenWidth/1.2, segmentHeight);
    self.segment.selectedSegmentIndex = 0;
    //segment添加点击触发方法
    [self.segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segment];
    self.segment.tintColor = [UIColor clearColor];//去掉颜色,现在整个segment都看不见
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
                                             NSForegroundColorAttributeName: [UIColor blackColor]};
    [self.segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
                                               NSForegroundColorAttributeName: [UIColor grayColor]};
    [self.segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    
    if (self.segment.selectedSegmentIndex == 0) {
        [self setLeftBarButtonItem];
    }
    
    // segment按钮效果
    self.segView = [[UIView alloc]initWithFrame:CGRectMake(10, segmentHeight - 2, self.segment.frame.size.width/3-20, 2)];
    self.segView.backgroundColor = [UIColor grayColor];
    [self.segment addSubview:_segView];
    
  
    
}

#pragma mark - 重写leftBarButtonItem
- (void)setLeftBarButtonItem
{
    
    self.placeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.placeButton.frame = CGRectMake(0, 0, 50*KScreenWidth/375, 30*KScreenWidth/375);
    [self.placeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.cityModel = [[CityModel alloc]init];
    self.cityModel.name = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];
    if (self.cityModel.name)
    {
        [self.placeButton setTitle:self.cityModel.name forState:UIControlStateNormal];
    }
    else
    {
        [self placeButtonAction];
        return;
    }

    
    [self.placeButton setTitle:self.cityModel.name forState:UIControlStateNormal];


    [self.placeButton addTarget:self action:@selector(placeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButton1 = [[UIBarButtonItem alloc]initWithCustomView:self.placeButton];
    
    self.imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.imageButton.frame = CGRectMake(0, 0, 30*KScreenWidth/375, 30*KScreenWidth/375);
    [self.imageButton setImage:[UIImage imageNamed:@"xia"] forState:UIControlStateNormal];
    [self.imageButton addTarget:self action:@selector(placeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButton2 = [[UIBarButtonItem alloc]initWithCustomView:self.imageButton];
    self.navigationItem.leftBarButtonItems=@[leftButton1,leftButton2];
}

#pragma mark----------验证是否选取地址------------------
-(void)judgePlace
{
    self.cityModel = [[NSUserDefaults standardUserDefaults]objectForKey:@"CityModel"];
    if (self.cityModel==nil)
    {
        PlaceViewController *placeVc =[[PlaceViewController alloc]init];
        [self.navigationController presentViewController:placeVc animated:YES completion:nil];
    }
    return;
}




#pragma mark-----------scrollView的触发方法------------
-(void)segmentAction:(UISegmentedControl *)segment
{
    NSInteger i = segment.selectedSegmentIndex;
    [self.scrollView setContentOffset:CGPointMake(KScreenWidth*i, 0) animated:YES];
    
    // 第一页显示leftBarButton，其他不显示
    CGFloat segWidth = KScreenWidth / 1.2 / 3;
    if (i == 0) {
        [self setLeftBarButtonItem];
        self.segView.frame = CGRectMake(0, segmentHeight - 2, segWidth, 2);
    }
    else if (i == 1){
        self.navigationItem.leftBarButtonItems = nil;
        self.segView.frame = CGRectMake(segWidth, segmentHeight - 2, segWidth, 2);
    }
    else if (i == 2){
        self.navigationItem.leftBarButtonItems = nil;
        self.segView.frame = CGRectMake(segWidth * 2, segmentHeight -2, segWidth, 2);
    }
}

#pragma mark----------scrollView的代理方法----------
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    self.segment.selectedSegmentIndex = point.x/KScreenWidth;
    
    // 第一页显示leftBarButton，其他不显示
    CGFloat segWidth = KScreenWidth / 1.2 / 3;
    if (self.segment.selectedSegmentIndex == 0) {
        [self setLeftBarButtonItem];
        self.segView.frame = CGRectMake(0, segmentHeight - 2, segWidth, 2);
    }
    else if (self.segment.selectedSegmentIndex == 1){
        self.navigationItem.leftBarButtonItems = nil;
        self.segView.frame = CGRectMake(segWidth, segmentHeight - 2, segWidth, 2);
    }
    else if (self.segment.selectedSegmentIndex == 2){
        self.navigationItem.leftBarButtonItems = nil;
        self.segView.frame = CGRectMake(segWidth * 2, segmentHeight - 2, segWidth, 2);
    }
}


#pragma mark---------左边按钮的触发方法-----------------
-(void)placeButtonAction
{
    PlaceViewController *place = [[PlaceViewController alloc]init];
    __weak typeof(self)vc = self;
    place.block=^(CityModel *cityModel)
    {

        vc.cityModel =cityModel;
        [vc.placeButton setTitle:cityModel.name forState:UIControlStateNormal];
    };
    [self.navigationController presentViewController:place animated:YES completion:nil];
}



@end
